<?php

namespace app\common\model;

use app\admin\model\shop\SkuGoods;
use app\common\model\integral\Goods;
use app\common\model\shop\SkuGoods as ShopSkuGoods;
use app\common\model\shop\SkuProduct;
use think\Db;
use think\Model;


class Cart extends Model
{





    // 表名
    protected $name = 'cart';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'tab_text'
    ];



    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getTabList()
    {
        return ['1' => __('Tab 1'), '2' => __('Tab 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTabTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tab']) ? $data['tab'] : '');
        $list = $this->getTabList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function product()
    {
        return $this->belongsTo(SkuProduct::class, 'product_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }
    public function goods()
    {
        return $this->belongsTo(SkuGoods::class, 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function integralgoods()
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    /**
     * Notes:购物车操作
     * @param int $user_id  用户id
     * @param int $num 变动数量
     * @param int $add_sub  变动类型：0-编辑；1-增加；2-减少
     * @param int $type 1=购物车购买,2=立即购买
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    static function add($data, $tab, $add_sub, $type = 1)
    {
        extract($data);
        if (!isset($num) || !$num) {
            $num = 1;
        }
        $goods = ShopSkuGoods::get($goods_id);
        if(!isset($goods)||!$goods){
            exception('商品不存在');
        }
        $userInfo = User::get($user_id);
        if ($userInfo->level <= 0 && $goods['type'] == 3) {
            exception('非会员不可购买一折专区商品');
        }
        Db::startTrans();
        try {
            $cart = self::get(['user_id' => $user_id,'type' => $type, 'goods_id' => $goods_id, 'product_id' => $product_id]);
            if ($cart) {
                switch ($add_sub) {
                    case 0:
                        $cart->num = $num;
                        $cart->save();
                        break;
                    case 1:
                        if ($type == 2) {
                            $cart->num = $num;
                            $cart->save();
                        } else {
                            $cart->setInc('num', $num);
                        }
                        break;
                    case 2:
                        $cart->setDec('num', $num);
                        break;
                }
                if ($cart->num <= 0) {
                    $cart->delete();
                }
            } else {
                $data = [
                    'user_id' => $user_id,
                    'goods_id' => $goods_id,
                    'product_id' => $product_id ?? 0,
                    'num' => $num,
                    'type' => $type,
                    'tab' => $tab,
                ];
                $cart = self::create($data, true);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            exception($e->getMessage());
        }
        return $cart->id;
    }
}
